<?php


require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/SReg.php";


$loginFromPage = 'index.php';
require_once '../../base.php';

clearstatcache();
if (is_file(DP_BASE_DIR . '/includes/config.php')) {
	require_once DP_BASE_DIR . '/includes/config.php';

} else {
	echo ('<html><head><meta http-equiv="refresh" content="5; URL=' . DP_BASE_URL 
	      . '/install/index.php"></head><body>' 
	      . 'Fatal Error. You haven\'t created a config file yet.<br/>' 
	      . '<a href="./install/index.php">Click Here To Start Installation and Create One!</a>' 
	      . ' (forwarded in 5 sec.)</body></html>');
	exit();
}

if (!(isset($GLOBALS['OS_WIN']))) {
	$GLOBALS['OS_WIN'] = (mb_stristr(PHP_OS, 'WIN') !== false);
}


// tweak for pathname consistence on windows machines
require_once (DP_BASE_DIR . '/includes/main_functions.php');
require_once (DP_BASE_DIR . '/includes/db_adodb.php');
require_once (DP_BASE_DIR . '/includes/db_connect.php');

require_once (DP_BASE_DIR . '/classes/ui.class.php');
require_once (DP_BASE_DIR . '/classes/permissions.class.php');
require_once (DP_BASE_DIR . '/includes/session.php');

// start session (needed for YADIS)
//session_start();

$AppUI=new CAppUI();
require_once($AppUI->getSystemClass('libmail'));
	include $AppUI->getModuleClass('contacts');
        include $AppUI->getModuleClass('admin');
require_once "functions.php";



// create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');

// create OpenID consumer
// read response from OpenID provider
$consumer = new Auth_OpenID_Consumer($store);
$response = $consumer->complete('http://localhost/t/dotproject/modules/auth/complete.php');

//dpSessionStart();

// set session variable depending on authentication result
if ($response->status == Auth_OpenID_SUCCESS) {
  
  
  // get registration information
  $sreg = new Auth_OpenID_SRegResponse();
  $obj = $sreg->fromSuccessResponse($response);
  $data = $response->message->args->values;
  $fname=$data[11];
  $email=$data[13];
  $lname=$data[15];

	$user_id;
	if(!($user_id=get_user_id($email))){
		//create the user
		create_user($email,$fname,$lname);
		$user_id=get_user_id($email);
	}

	session_start();
	$_SESSION['user_id']=$user_id;

} 

?>

<html>
	<script type="text/javascript">
		window.close();
	</script>
</html>